package tt.dz.order.pojo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tt.dz.order.dao.BaseDAOUtil;
import tt.dz.order.exception.BusinessException;

/**
 * @Title: 单价计量单位
 * @author zhoutian   
 * @date 2015-11-26
 */
public class OrderUnit {
	//计量单位编号（主键，程序自增）
	private Integer unitNo;
	//计量单位名称
	private String name;
	//计量单位名称描述
	private String description;
	//有效标志  1:有效  0：无效
	private Integer isValid;
	//创建人
	private String creator;
	//创建时间
	private Date createTime;
	
	public Integer getUnitNo() {
		return unitNo;
	}

	public void setUnitNo(Integer unitNo) {
		this.unitNo = unitNo;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Integer getIsValid() {
		return isValid;
	}

	public void setIsValid(Integer isValid) {
		this.isValid = isValid;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public OrderUnit() {}
	
	private static final String select_all_sql = "select unitNo, name, description, isValid, creator, createTime from Order_Unit";
	
	private static final String update_all_sql = "update Order_Unit set unitNo = :unitNo, name = :name, description = :description, isValid = :isValid, creator = :creator, createTime = :createTime";
	
	public void save() throws BusinessException{
		if(isUnitNameExist(-1, this.name)){
			throw new BusinessException("单位名称重复");
		}
		String sql = "insert into Order_Unit(unitNo, name, description, isValid, creator, createTime) values(:unitNo, :name, :description, :isValid, :creator, :createTime)";
		BaseDAOUtil.updateObject(sql, this);
	}
	
	public void update() throws BusinessException{
		if(isUnitNameExist(this.unitNo, this.name)){
			throw new BusinessException("单位名称重复");
		}
		String sql = update_all_sql + " where unitNo = :unitNo";
		BaseDAOUtil.updateObject(sql, this);
	}
	
	public void delete(){
		String sql = "delete from Order_Unit where unitNo = :unitNo";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("unitNo", this.unitNo);
		BaseDAOUtil.delete(sql, paramMap);
	}
	
	public static boolean isUnitNameExist(Integer unitNo, String name){
		String sql = select_all_sql + " where unitNo != :unitNo and name = :name";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("unitNo", unitNo);
		paramMap.put("name", name);
		List<OrderServiceInfo> list = BaseDAOUtil.queryForList(sql, paramMap, OrderServiceInfo.class);
		if(list != null && list.size() > 0){
			return true;
		}
		return false;
	}
	
	public static List<OrderUnit> getAll(){
		String sql = select_all_sql + " order by unitNo asc";
		return BaseDAOUtil.queryForList(sql, new HashMap<String, Object>(), OrderUnit.class);
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static OrderUnit getBy(Integer unitNo){
		String sql = select_all_sql + " where unitNo = :unitNo";
		Map paramMap = new HashMap();
		paramMap.put("unitNo", unitNo);
		return BaseDAOUtil.queryForObject(sql, paramMap, OrderUnit.class);
	}
	
	public static Integer getMaxUnitNo(){
		String sql = "select max(unitNo) as max from Order_Unit";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		List<Map<String, Object>> unitNoList = BaseDAOUtil.queryForList(sql, paramMap);
		if(unitNoList == null || unitNoList.size() == 0){
			return 0;
		}
		Map<String, Object> resultMap = unitNoList.get(0);
		Number num = (Number) resultMap.get("max");
		if (num == null) {
			return 0;
		}
		return num.intValue();
	}
}
